<?php
    defined( 'BASEPATH' ) OR exit( 'No direct script access allowed' );

    /**
     * ------------------------------
     * | Class MY_Controller 全站总控制器
     * ------------------------------
     */
    class MY_Controller extends CI_Controller
    {
        public function __construct()
        {
            parent::__construct();

            #渲染模板
            $this->load->library( 'layout' );
        }
    }

    /**
     * ----------------------------------------
     * |Class Front_Controller 前台控制器
     * ----------------------------------------
     */
    class Front_Controller extends MY_Controller
    {
        public function __construct()
        {
            parent::__construct();
            $this->load->front_views();
            //$this->layout->layout(['global_for_layout'=>'global_layout'],['username'=>'luxiaofei']);
            $this->layout->layout( [ 'global_for_layout' => 'global_layout' ], [ 'username' => 'luxiaofei' ] );
        }
    }

    /**
     * ----------------------------------------
     * |Class Pc_Controller pc端总控制器
     * ----------------------------------------
     */
    class Pc_Controller extends Front_Controller
    {
        public function __construct()
        {
            parent::__construct();

            #渲染模板
            //$this->layout->layout(['global_for_layout'=>'global_layout'],['username'=>'luxiaofei']);
            $this->layout->layout( [ 'global_for_layout' => 'global_layout' ], [ 'username' => 'luxiaofei' ] );
        }
    }

    /**
     * ----------------------------------------
     * |Class Mobile_Controller 移动端总控制器
     * ----------------------------------------
     */
    class Mobile_Controller extends Front_Controller
    {
        public function __construct()
        {
            parent::__construct();

            #渲染模板

            //$this->layout->layout(['mobile_for_layout'=>'mobile/mobile_layout'],['username'=>'luxiaofei']);
            /*$this->layout->layout( [ 'model_for_layout' => 'home/home_layout' ], [ 'displayname' => 'xiaofei' ] );*/
        }
    }

    /**
     * ----------------------------------------
     * |Class Mobile_Controller 移动端总控制器
     * ----------------------------------------
     */
    class Mobile_Controller_bak_20150908 extends Front_Controller
    {
        public function __construct()
        {
            parent::__construct();

            #渲染模板

            //$this->layout->layout(['mobile_for_layout'=>'mobile/mobile_layout'],['username'=>'luxiaofei']);
        }
    }

    /**
     * ----------------------------------------
     * |Class Home_Controller 前台控制器
     * ----------------------------------------
     */
    class Home_Controller extends Pc_Controller
    {
        public function __construct()
        {
            parent::__construct();

            //$this->layout->layout(['model_for_layout'=>'home/home_layout'],['displayname'=>'xiaofei']);
            $this->layout->layout( [ 'model_for_layout' => 'home/home_layout' ], [ 'displayname' => 'xiaofei' ] );

        }
    }

    /**
     * ------------------------------------------------------
     * | Class Wap_Controller wap控制器
     * ------------------------------------------------------
     */
    class Wap_Controller extends MY_Controller
    {
        public function __construct()
        {
            parent::__construct();
            //exit("wap正在努力开发中。。。");
            //$this->layout->layout(['model_for_layout'=>'wap/wap_layout'],['username'=>'luxiaofei']);
            $this->layout->layout( [ 'model_for_layout' => 'wap/wap_layout' ], [ 'username' => 'luxiaofei' ] );
        }
    }
    /**
     * ------------------------------------------------------
     * | Class Wap_Controller wap控制器
     * ------------------------------------------------------
     */
    class Wap_Controller_bak_20150908 extends Mobile_Controller
    {
        public function __construct()
        {
            parent::__construct();
            //exit("wap正在努力开发中。。。");
            //$this->layout->layout(['model_for_layout'=>'wap/wap_layout'],['username'=>'luxiaofei']);
            $this->layout->layout( [ 'model_for_layout' => 'wap/wap_layout' ], [ 'username' => 'luxiaofei' ] );
        }
    }
    /**
     * ------------------------------------------------------
     * | Class App_Controller app控制器
     * ------------------------------------------------------
     */
    class App_Controller extends Mobile_Controller
    {
        public function __construct()
        {
            parent::__construct();
            //exit( "app正在努力开发中。。。" );
            //$this->layout->layout(['model_for_layout'=>'mobile/mobile_layout'],['displayname'=>'xiaofei']);
            //$data['username']="luxiaofei";
        }
    }


    /**
     * --------------------------------------------------
     * |Class Admin_Controller 后台控制器
     * --------------------------------------------------
     */
    class Admin_Controller extends MY_Controller
    {
        public function __construct()
        {
            parent::__construct();
            #渲染后台模板
            $this->load->library( 'layout' );

            #验证权限
            $aid = $this->session->userdata( 'admin_id' );
            $aname = $this->session->userdata( 'admin_name' );
            //如果没有SESSION则跳转到登录
            if ( !$aid ) redirect( "admin/privilege/login" );

            #进行权限认证
            if ( !$_SESSION[ 'in_super_group' ] ) {
                if ( empty( $_SESSION[ 'auth_url' ] ) ) {
                    $this->load->model( "admin/admin_model" );
                    $_SESSION[ 'auth_url' ] = $this->admin_model->getOperateNodesByAdminId( ['uid'=>$aid] );
                }
                //超级对象
                $ci = &get_instance();
                //分组目录
                $directory = substr( $ci->router->directory, 0, -1 );
                //当前控制器
                $controller = $ci->router->class;
                //当前方法
                $method = $ci->router->method;
                //当前url
                $curren_url = $directory . "/" . $controller . "/" . $method;
                $url_tag = false;
                $url_condition = '';
                foreach ( $_SESSION[ 'auth_url' ] as $k => $v ) {
                    if ( strtolower( $curren_url ) == strtolower( $v[ 'url' ] ) ) {
                        $url_condition = $v[ 'condition' ];
                        $url_tag = true;
                        break;
                    }
                }
                #如果对应url中有条件
                if ( $url_condition ) {
                    $url_condition = explode( "&", $url_condition );
                    $_GET[ '_url_condition' ] = $url_condition;
                    $condition = [ ];
                    foreach ( $url_condition as $k => &$v ) {
                        //$condition_str=preg_replace('/([\w_].*)([\<\=\>\!]+?)([\w_].*)/','$_GET[\'\\1\']=\'\\2\\3\'',$v);
                        $condition_str = preg_replace( '/([\w_].*)([\<\=\>\!]+?)([\w_].*)/', '$condition[\'\\1\']=\'\\2\\3\'', $v );
                        @eval( "\$com = " . $condition_str . '; ' );
                    }
                    $_GET[ 'url_condition' ] = $condition;
                }

                #如果没有匹配到url
                if ( !$url_tag ) P::exitError( "您对" . $curren_url . "节点没有操作权限" );
            }

            #格式化全局分页
            $page = !isset( $_POST[ 'page' ] ) || $_POST[ 'page' ] <= 1 ? 1 : $_POST[ 'page' ];
            define( 'GLOBAL_PAGE', $page );
            $rows = !isset( $_POST[ 'rows' ] ) || $_POST[ 'rows' ] <= 1 ? 20 : $_POST[ 'rows' ];
            define( 'GLOBAL_ROWS', $rows );
            $start_row = ( $page - 1 ) * $rows;
            define( 'GLOBAL_START_ROW', $start_row );

            #定义全局变量

            //$this->layout->layout(['model_for_layout'=>'admin_layout'],['displayname'=>'xiaofei']);
            $this->layout->layout( [ 'model_for_layout' => 'admin_layout' ], [ 'displayname' => 'xiaofei' ] );
        }
    }


    class Option_Controller extends Front_Controller
    {
        public function __construct()
        {
            parent::__construct();
            #渲染模板
            //$this->layout->layout(['global_for_layout'=>'global_layout'],['username'=>'luxiaofei']);
            $this->layout->layout( [ 'global_for_layout' => 'global_layout' ], [ 'username' => 'luxiaofei' ] );
        }
    }

?>